﻿#include "MyCustomListWidget.h"
#include <QResizeEvent>

MyCustomListWidget::MyCustomListWidget(QWidget *parent) : QListWidget(parent)
{

}

void MyCustomListWidget::resizeEvent(QResizeEvent *event)
{
    //itemWidgth的宽度必须略小于 QListWidget 的宽度 除 三。否则会出现异常情况(死循环造成的？)
    //2 是在作者机器上测出来 最小 不出现异常情况的数值
    int itemWidth = event->size().width() / 3 - (3 + 1) * this->spacing() - 2;
    int itemHeight = event->size().height() / 2 - 3;

    for(int i = 0; i < this->count(); i++)
    {
        QWidget *widget = this->itemWidget(this->item(i));
        widget->resize(itemWidth, itemHeight);
        this->item(i)->setSizeHint(QSize(itemWidth, itemHeight));
    }
}
